﻿package model.entity.product;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import model.business.product.ProductState;
import model.entity.Brand;

@Entity
@Table(name="Product")
public abstract class Product {

	@Id
	@GeneratedValue(generator = "Product_ID_Gen", strategy = GenerationType.IDENTITY)
	@SequenceGenerator(name = "Product_ID_Gen", sequenceName = "Product_SEQ_ID")
	protected int id;
	
	@Column(nullable=false)
	protected String name;

	@Column(nullable=false)
	protected int price;

	protected String image;
	protected int quantity;
	protected String extraInfo;

	@ManyToOne
	protected Brand brand;
	protected ProductState state;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getPrice() {
		return price;
	}
	public void setPrice(int price) {
		this.price = price;
	}
	public String getImage() {
		return image;
	}
	public void setImage(String image) {
		this.image = image;
	}
	public Integer getQuantity() {
		return quantity;
	}
	public void setQuantity(Integer quantity) {
		this.quantity = quantity;
	}
	public String getExtraInfo() {
		return extraInfo;
	}
	public void setExtraInfo(String extraInfo) {
		this.extraInfo = extraInfo;
	}
	public Brand getBrand() {
		return brand;
	}
	public void setBrand(Brand brand) {
		this.brand = brand;
	}
	public ProductState getState() {
		return state;
	}
	public void setState(ProductState state) {
		this.state = state;
	}
	public int getId() {
		return id;
	}
	@Override
	public String toString() {
		return "Product [id=" + id + ", name=" + name + ", price=" + price
				+ ", image=" + image + ", quantity=" + quantity
				+ ", extraInfo=" + extraInfo + ", brand=" + brand + ", state="
				+ state + "]";
	}
	
	public static Map<String, Class<?>> fieldMap(){
		Map<String, Class<?>> fieldMap = new HashMap<>();
        fieldMap.put("id", int.class);
        fieldMap.put("name", String.class);
        fieldMap.put("price", int.class);
        fieldMap.put("image", String.class);
        fieldMap.put("quantity", int.class);
        fieldMap.put("extraInfo", String.class);
        fieldMap.put("brand", Brand.class);
        fieldMap.put("state", ProductState.class);
        return fieldMap;
	}
	
	public void insertData(Map<String, Object> valueMap){
		for (Entry<String, Object> entry : valueMap.entrySet()) {
			Object value = entry.getValue();
			
			//Dk rong
			if(entry.getValue() == null) continue;
			
			switch (entry.getKey()) {
			case "id":
				if(this.id == 0) this.id = (int) value;
				break;
			case "name":
				this.name = (String) value;
				break;
			case "image":
				this.image = (String) value;
				break;
			case "extraInfo":
				this.extraInfo = (String) value;
				break;
			case "price":
				this.price = (int) value;
				break;
			case "quantity":
				this.quantity = (int) value;
				break;
			case "brand":
				this.brand = (Brand) value;
				break;
			case "state":
				this.state = (ProductState) value;
				break;

			default:
				break;
			}
		}
	}

}
